const logger = require('./logger.lib');
const _ = require('lodash');

class Promise {
    constructor() { }
    createPromise(...args) {
        let method = args.shift();
        return new global.Promise((resolve, reject) => {
            
            //http回调函数处理
            args.push((res) => {
                let obj = args[0],
                    data = JSON.parse(res).data || {};
                let path = _.isString(obj) ? obj : obj.path;
                logger.data.debug(`请求接口:【${path}】`);
                logger.data.debug(`接口数据:【${res}】`);
                resolve(data);
            });

            //代理函数
            method.apply(this, args);
        })
    }
    all(array) {
        return global.Promise.all(array).catch(err => {
            logger.error(`promise all error 【${err}】`);
        })
    }
    resolve(params) {
        return global.Promise.resolve(params);
    }
    reject(params) {
        return global.Promise.reject(params);
    }
}

module.exports = Promise;
